package com.example.translateappjava.Activity.HomeFragmentActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.translateappjava.Activity.MainActivity;
import com.example.translateappjava.R;
import com.example.translateappjava.Singleton.GlobalData;

public class BadResultActivity extends AppCompatActivity {

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_bad_result);
        Bitmap bitmap = null;
        Intent intent = getIntent();
        byte[] byteArray = intent.getByteArrayExtra("bitmap");

        // 获取从 DiabetesFoodAdvisor 传递过来的科学依据信息
        String scientificBasis = getIntent().getStringExtra("scientificBasis");
        String recognitionResult = getIntent().getStringExtra("recognitionResult");
        if (byteArray != null) {
            bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
            // 使用 bitmap
        }
        // 找到布局中的 TextView 并设置文本
        @SuppressLint({"MissingInflatedId", "LocalSuppress"}) TextView textViewScientificBasis = findViewById(R.id.badreasons);
        textViewScientificBasis.setText("   "+scientificBasis + "\n\n\n\n\n");

        @SuppressLint({"MissingInflatedId", "LocalSuppress"}) TextView fruitname = findViewById(R.id.tv_top);
        fruitname.setText(recognitionResult);

        GlobalData globalData = GlobalData.getInstance();
        String value = globalData.getGlobalVariable();

        TextView warn = findViewById(R.id.question);
        warn.setText("这是"+value+"不建议食用的食物，你是否要继续食用？");


        if(bitmap != null){
            ImageView imageView = findViewById(R.id.imageView2);
            imageView.setImageBitmap(bitmap);
        }

        /**
         * 为按钮选择增加点击事件
         */
        Button eat = findViewById(R.id.button2);
        Button noteat = findViewById(R.id.button3);

        eat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showNoDialog();
            }
        });

        noteat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showYesDialog();
            }
        });


        findViewById(R.id.btn_exit3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(BadResultActivity.this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                finish();
            }
        });
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });


    }
    /**
     * 设置返回逻辑
     */
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent intent = new Intent(BadResultActivity.this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish(); // 结束当前 Activity
    }

    /**
     * 这里是显示点击按钮后的事件
     */

    private void showNoDialog() {
        // 使用Activity的context
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        // 获取LayoutInflater实例
        LayoutInflater inflater = getLayoutInflater();
        // 使用inflater加载自定义布局
        View dialogView = inflater.inflate(R.layout.eat, null);
        // 设置自定义布局
        builder.setView(dialogView);

        // 添加按钮或其他控件的事件处理
        builder.setPositiveButton("OK", (dialog, which) -> {
            // 处理点击事件
//            Toast.makeText(this, "OK clicked", Toast.LENGTH_SHORT).show();
        });

        builder.setNegativeButton("Cancel", (dialog, which) -> {
            dialog.dismiss();
        });

        // 创建并显示对话框
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    private void showYesDialog() {
        // 使用Activity的context
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        // 获取LayoutInflater实例
        LayoutInflater inflater = getLayoutInflater();
        // 使用inflater加载自定义布局
        View dialogView = inflater.inflate(R.layout.noeat, null);
        // 设置自定义布局
        builder.setView(dialogView);

        // 添加按钮或其他控件的事件处理
        builder.setPositiveButton("OK", (dialog, which) -> {
            // 处理点击事件
//            Toast.makeText(this, "OK clicked", Toast.LENGTH_SHORT).show();
        });

        builder.setNegativeButton("Cancel", (dialog, which) -> {
            dialog.dismiss();
        });

        // 创建并显示对话框
        AlertDialog dialog = builder.create();
        dialog.show();
    }

}
